package com.genloong.flycloud.filecenter.message;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.genloong.flycloud.filecenter.entity.BizFile;
import com.genloong.flycloud.filecenter.entity.SysConfig;
import com.genloong.flycloud.filecenter.entity.SysLog;
import com.genloong.flycloud.filecenter.service.SysConfigService;
import com.genloong.flycloud.filecenter.service.SysLogService;
import com.rabbitmq.client.Channel;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;


/**
 * 接收消息
 */
@Log
@Component
@Configuration
public class ReceiveMessage {

    @Autowired
    MessageServer messageServer;
    @Autowired
    SysConfigService sysConfigService;
    @Autowired
    SysLogService sysLogService;

    /**
     * @param msg
     * @param channel 写情报板
     */
    @RabbitHandler
    @RabbitListener(bindings = @QueueBinding(value = @Queue(autoDelete = "true", durable = "false"),
            exchange = @Exchange(value = "selfData", autoDelete = "true",type = ExchangeTypes.DIRECT), key = "file_center_dowaload_file"),concurrency = "20")
    public void onWrite(Message msg, Channel channel) {

        try {
            String receiveMessage = new String(msg.getBody(), "UTF-8");
            BizFile bizFile = JSON.parseObject(receiveMessage, BizFile.class);
            SysConfig file_save_path_obj = sysConfigService.getOne(new QueryWrapper<SysConfig>()
                    .eq("config_name","file_save_path"));
            OkHttpUtils.get()
                    .url(bizFile.getFilePath())
                    .build()
                    .execute(new FileCallBack(file_save_path_obj.getConfigValue(),bizFile.getFileName()) {
                        @Override
                        public void onError(Call call, Exception e, int i) {
                            SysLog entity = new SysLog();
                            String filePath = bizFile.getFilePath();
                            entity.setContent("文件下载失败："+ filePath);
                            sysLogService.save(entity);
                            log.info("onError:"+filePath);
                            e.fillInStackTrace();
                        }

                        @Override
                        public void onResponse(File file, int i) {
                            SysLog entity = new SysLog();
                            entity.setContent("文件下载成功："+bizFile.getFilePath());
                            sysLogService.save(entity);
                        }
                    });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }











}
